<?php

class PhotoController extends Base_Controller
{

    public function init()
    {
        /* Initialize action controller here */
    	parent::init();
    }

    /*public function indexAction()
    {
        // action body
        $this->logger->debug("Entering  ".__METHOD__);
		//$this->_helper->layout->disableLayout();
		$photoService = Application_Model_PhotoService::getInstance();
		$config = Zend_Registry::get("config");		
		$relativePhotoDir = $config->get("photo")->get("path");
		$photoDir = $_SERVER['DOCUMENT_ROOT']. $relativePhotoDir;
		$this->logger->debug("photoDir  ".$photoDir);
		$albums = $photoService->getFolders($photoDir);		
    	$_SESSION['albums']=$albums;
		$this->view->albums = $albums;
    }*/
    
	public function indexAction()
    {
        // action body
        $this->logger->debug("Entering  ".__METHOD__);
		//$this->_helper->layout->disableLayout();
		$photoService = Application_Model_PhotoService::getInstance();
		$userid = null;
		if ($this->uid != null) {
			$userid = $this->$uid;
		} else {
			$userid = $this->getLoginUid();
		}
		$albums = $photoService->getAlbums($userid);
		$this->view->albums = $albums;
    }
    
    public function addalbumAction()
    {
    	//nothing here
    	
    }
    
 	public function savealbumAction()
    {
       if($this->isLogin()) {
    	$uid = $this->getLoginUid();
        
    	$request = $this->getRequest();
        if($this->getRequest()->isPost())
        {	
        	$desc = "";
        	if ($_POST['desc'] != null ) {
        		$desc = $_POST['desc'];
        	}
        	
        	$album = Array(
        		"uid" => $uid,
        		"album_name" =>$_POST['name'],
        		"desc" =>$desc,
        	); 
        	
        	$photoService = Application_Model_PhotoService::getInstance();
        	
        	if ($_POST['albumId'] != null && $_POST['albumId'] !="") {
        		$album['album_id'] = $_POST['albumId'];
        	} else {        	
        		$photoService->	createAlbum($album);	
        	}        				
        }
      } 
      $this->_redirect("/photo");    
    }
    
    public function albumAction()
    {
        // action body
        $this->logger->debug("Entering  ".__METHOD__);
        $albumIdx = $_GET['album'];
    	if(isset($_SESSION['albums'])) {
    		$albums = $_SESSION['albums'];
    	}
    	$album=$albums[$albumIdx];
		$photoService = Application_Model_PhotoService::getInstance();
		$config = Zend_Registry::get("config");		
		$relativePhotoDir = $config->get("photo")->get("path")."/".$album;
		$photoDir = $_SERVER['DOCUMENT_ROOT']. $relativePhotoDir;
		$this->logger->debug("photoDir  ".$photoDir);
		$files = $photoService->getAllFiles($photoDir, "/.jpg/i");
		foreach ($files as $file) {
			is_dir($photoDir."/thumb_big")||mkdir($photoDir."/thumb_big");
			is_dir($photoDir."/thumb_small")||mkdir($photoDir."/thumb_small");
			is_file($photoDir."/thumb_big".'/'.$file)||
			$photoService->genThumbs($photoDir."/".$file, 680, 450, 0, $photoDir."/thumb_big".'/'.$file);
			is_file($photoDir."/thumb_small".'/'.$file)||
			$photoService->genThumbs($photoDir."/".$file, 75, 75, 1, $photoDir."/thumb_small".'/'.$file);
		}		
		$this->view->files = $files;
		$this->view->bigDir=$relativePhotoDir."/thumb_big";
		$this->view->smallDir=$relativePhotoDir."/thumb_small";
		$this->view->albums = $albums;
    }

}

